<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of home
 *
 * @author peerawatj20
 */
class home extends MY_Controller {

    private $license_code = '';
    private $brand = '';
    private $model = '';
    private $moter_code = '';

    public function __construct() {
        parent::__construct();
        $this->load->model('truck_model');
        $this->load->model('catalog');
    }

    public function index() {

        $this->session->unset_userdata('search_license_code');
        $this->session->unset_userdata('search_brand');
        $this->session->unset_userdata('search_model');
        $this->session->unset_userdata('search_moter_code');

        $config['base_url'] = base_url() . "/truck/home/search";
        $config['total_rows'] = $this->truck_model->count();
        $config['per_page'] = 10;
        $config['uri_segment'] = 4;
        $utility = new Config_form();
        $config = $utility->form_pagination($config);
        $this->pagination->initialize($config);

        $result = $this->truck_model->search_list_truck();
        $this->data['result_list'] = $result;
        $this->data['num_list'] = $this->uri->segment(4);
        $this->_set_form();
        $this->load->view('truck/index', $this->data);
    }

    private function _set_form() {
        $this->data['txt_license_code'] = form_input('txtLicenseCode', $this->license_code, 'class="form-control" placeholder="ทะเบียน"');
        $this->data['cmb_brand'] = form_dropdown('cmbBrand', $this->catalog->list_truck_brand(), $this->brand, 'class="form-control" id="cmbBrand" style="width:150px"');
        $this->data['cmb_model'] = form_dropdown('cmbModel', $this->catalog->list_truck_model($this->session->userdata('search_brand')), $this->model, 'class="form-control"  id="cmbModel" style="width:150px"');
        $this->data['txt_moter_code'] = form_input('txtMoterCode', $this->moter_code, 'class="form-control" placeholder="หมายเลขเครื่อง"');
    }

    public function search() {
        //Clean Session for searching and it come from search btn.
        if ($this->input->post('action') == 'search') {
            $this->session->unset_userdata('search_license_code');
            $this->session->unset_userdata('search_brand');
            $this->session->unset_userdata('search_model');
            $this->session->unset_userdata('search_moter_code');

            $this->license_code = $this->input->post('txtLicenseCode');
            $this->brand = $this->input->post('cmbBrand');
            $this->model = $this->input->post('cmbModel');
            $this->moter_code = $this->input->post('txtMoterCode');

            $this->session->set_userdata('search_license_code', $this->license_code);
            $this->session->set_userdata('search_brand', $this->brand);
            $this->session->set_userdata('search_model', $this->model);
            $this->session->set_userdata('search_moter_code', $this->moter_code);
        } else {
            //set value for pagination.
            $this->license_code = $this->session->userdata('search_license_code');
            $this->brand = $this->session->userdata('search_brand');
            $this->model = $this->session->userdata('search_model');
            $this->moter_code = $this->session->userdata('search_moter_code');
        }

        $config['base_url'] = base_url() . "/truck/home/search";
        $config['total_rows'] = $this->truck_model->count($this->license_code, $this->brand, $this->model, $this->moter_code);
        $config['per_page'] = 10;
        $config['uri_segment'] = 4;
        $utility = new Config_form();
        $config = $utility->form_pagination($config);
        $this->pagination->initialize($config);

        $result = $this->truck_model->search_list_truck($this->license_code, $this->brand, $this->model, $this->moter_code, $config['per_page'], $this->uri->segment(4));
        $this->data['result_list'] = $result;
        $this->data['num_list'] = $this->uri->segment(4);
        $this->_set_form();
        $this->load->view('truck/index', $this->data);
    }

}
